// pages/defect/flaw/index.js
import { list } from "../../../../../../api/ingredients";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    ingredientsList: {
      type: Array,
      value: [],
      observer(data) {
        this.setIngredientsList(data);
      },
    },
    dataList: {
      type: Array,
      value: [] 
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataSource: [],
    actived: null,
    _ingredientsList: [],
    loading: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setIngredientsList(data) {
      this.setData({
        dataSource: data,
      });
    },
    onParentClick(event) {
      const item = event.currentTarget.dataset.item;
      this.fetchIgnList(item.id);
      if (item.id === this.data.actived) {
        this.setData({
          actived: null,
        });
        return;
      }

      this.setData({
        actived: item.id,
      });
      // this.triggerEvent('select', item);
      // this.onCancel();
    },

    fetchIgnList(id) {
      this.setData({
        loading: true,
      });
      list(
        {
          parentId: id,
          status: "ANNOUNCED",
          currentPage: 1
        },
        (res) => {
          const _ = res.map(it=>{
            return {
              ...it,
              _disabled: this.properties.dataList.find(item=> {
                return item.ingredientsId === it.id
              }) !== undefined
            }
          })
          this.setData({
            _ingredientsList: _,
            loading: false,
          });
        }
      );
    },
    onCancel() {
      this.triggerEvent("cancel");
    },
    onSelectedData(event) {
      const item = event.currentTarget.dataset.item;
      this.triggerEvent("select", item);
      this.setData({
        actived: null,
      });
    },
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
    },
  },
});
